<?
include('wallet_db.html');

//------------------------------------------------------------------------------
function set_seq ($seq, $pr_id) {
	GLOBAL $id, $dbh81,$w;
	unset($query);
	$query = "UPDATE plan_features SET seq='$seq' WHERE reseller_id='$id' AND id='$pr_id'";
	$result= pg_query($dbh81,$query);
	#echo $query." ".$w."<br>";
}
//------------------------------------------------------------------------------
function get_id_for_seq($seq,$store_id) {
	unset($sql);
	GLOBAL $id, $dbh81;
        $sql['type']    = "SELECT";
        $sql['params']  = "id";
        $sql['table']   = "plan_features";
        $sql['where']   = "reseller_id='$id' AND store_id='$store_id' AND seq='$seq'";
        $sql['options'] = "";
        $sql['return']  = "";
        if (!$res = sql_exec($sql,$dbh81)) {
        	exit("ERROR1");
		} else {
        return($res[0]['id']);
       }
}
//------------------------------------------------------------------------------
function get_lower_seq($seq,$store_id) {
	unset($sql);
	GLOBAL $id, $dbh81;
        $sql['type']    = "SELECT";
        $sql['params']  = "seq";
        $sql['table']   = "plan_features";
        $sql['where']   = "reseller_id='$id' AND store_id='$store_id' AND seq < '$seq' ORDER BY seq DESC LIMIT 1";
        $sql['options'] = "";
        $sql['return']  = "";
        if (!$res = sql_exec($sql,$dbh81)) {
			exit("ERROR2");
		} else {
        return($res[0]['seq']);
       }
}
//------------------------------------------------------------------------------
function get_higher_seq($seq,$store_id) {
	unset($sql);
	GLOBAL $id, $dbh81;
        $sql['type']    = "SELECT";
        $sql['params']  = "seq";
        $sql['table']   = "plan_features";
        $sql['where']   = "reseller_id='$id' AND store_id='$store_id' AND seq > '$seq' ORDER BY seq LIMIT 1";
        $sql['options'] = "";
        $sql['return']  = "";
        if (!$res = sql_exec($sql,$dbh81)) {
		exit("ERROR3");
	} else {
        return($res[0]['seq']);
       }
}
//-------------------------------------------------------------------------------

// begin - da se mahne kato ima poveche stores i da se dava parametyr store_id za po-nadolu - paz - leto gospodne 2006, mesec fevruarii den 13...
//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='store_id';
	$sql['table']='stores';
	$sql['where']="reseller_id='$id'";
	$sql['options']='';
	$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	$errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit();
}
$store_id = $result[0]['store_id'];
// end

echo "<div align='left'>";
echo "<p><font class='text'><b>".translate('sort_features')."</b></p>
<p>".translate('sort_features_desc')."</p>";
unset($sql);
#echo $plan_id."<br><br>";
#echo "$move<br>";
if (isset($move) && $move == 'yes') {
	/*$w=1;
	echo "sid - "."$sid"."<br>";
	echo "action - "."$action"."<br>";
	echo "prod_id - "."$prod_id"."<br>";
	echo "min_seq - "."$pl_min_seq"."<br>";
	echo "max_seq - "."$pl_max_seq"."<br>";
	echo "cur_seq - "."$pl_cur_seq"."<br>";
	echo "move_pl - "."$move_pl"."<br>";
	*/
	switch ($move_pl) {
		case 'up':
		$w=2;
			$new_seq = get_lower_seq($pl_cur_seq,$store_id);
			if ($new_seq>0) {
				$w=3;
				$prod_id2 = get_id_for_seq($new_seq,$store_id);
				#echo "<br><b>debug - $prod_id to $new_seq ## $prod_id2 to $pl_cur_seq</b><br>";
				set_seq($pl_cur_seq, $prod_id2);
				set_seq($new_seq, $prod_id);
			}
			break;
		case 'down':
			$w=5;
			$new_seq = get_higher_seq($pl_cur_seq,$store_id);
    	    if ($new_seq >= $pl_min_seq) {
               $prod_id2 = get_id_for_seq($new_seq,$store_id);
                #echo "<br><b>debug - $prod_id to $new_seq ## $prod_id2 to $pl_cur_seq</b><br>";
                set_seq($pl_cur_seq, $prod_id2);
                set_seq($new_seq, $prod_id);
			}
			break;
	}
}
unset($move,$move_pl,$new_seq,$pl_cur_seq,$prod_id,$prod_id2,$min_seq,$max_seq,$pl_min_seq,$pl_max_seq,$sql);

//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='min(seq) as min, max(seq) as max';
	$sql['table']='plan_features';
	$sql['where']="reseller_id='$id' and store_id='$store_id'";
	$sql['options']='';
	$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	$errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit();
}
$min_seq = $result[0]['min'];
$max_seq = $result[0]['max'];
unset($sql);
//------------------------------------------------------
	$sql['type']='SELECT';
	$sql['params']='id,feature,seq';
	$sql['table']='plan_features';
	$sql['where']="reseller_id='$id' and store_id='$store_id'";
	$sql['options']='ORDER BY seq';
	$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	$errorMsg = translate('int_error');
	include_once('errorMsg.html');
	exit();
}

echo "<div align='center'><TABLE width='450' class='bTable' cellspacing='0' >";
foreach ($result as $value) {
	unset($para);
	$para['sid']=$sid;
	$para['action']='editDescription';
	$para['prod_id'] = $value['id'];
	$para['move']='yes';
	$para['pl_min_seq']=$min_seq;
	$para['pl_max_seq']=$max_seq;
	$para['plan_id'] = $plan_id;
	if ($value['seq']<$max_seq && $value['seq']>$min_seq) {
		$para['pl_cur_seq']=$value['seq'];
		$para['move_pl']='up';
		$movurl="$baseURL/$webDIR/?".encode_params($para);
		$uparrow="<a href='$movurl'><img src='images/arrow_red_up.gif' border=0></a>";
		$para['move_pl']='down';
		$movurl="$baseURL/$webDIR/?".encode_params($para);
		$downarrow="<a href='$movurl'><img src='images/arrow_red_down.gif' border=0></a>";
	} elseif ($value['seq']==$min_seq) {
		$para['pl_cur_seq']=$value['seq'];
		$para['move_pl']='down';
		$movurl="$baseURL/$webDIR/?".encode_params($para);
		$uparrow="<a href='$movurl'><img src='images/arrow_red_down.gif' border=0></a>";
		$downarrow="";
	} elseif ($value['seq']==$max_seq) {
		$para['pl_cur_seq']=$value['seq'];
		$para['move_pl']='up';
		$movurl="$baseURL/$webDIR/?".encode_params($para);
		$uparrow="";
		$downarrow="<a href='$movurl'><img src='images/arrow_red_up.gif' border=0></a>";
	}
	echo "<tr>
		<td width='50%' align='left'>".translate($value['feature'])."</td>
		<td width='50%' align='right'>$uparrow&nbsp;$downarrow</td>
	</tr>";
}
echo "</table></div>";
echo "</div>";
?>